﻿using System;
using System.Web;

namespace Core.Library
{
    public class Utils
    {
        public static string ControlId = "cId";
        public static string UserControlId = "ucId";
        public static string GroupId = "gId";
        public static string MenuId = "mId";
        public static string UserId = "uId";
        public static string Tab = "tab";
        public static string TypeId = "tId";
        public static string TimeType = "ttId";
        public static string Status = "status";
        public static string Fix = "fix";

        public static string CategoryId = "catId";
        public static string ArticleId = "aId";
        public static string ProductId = "pId";
        public static string OrderId = "oId";
        public static string OnlineId = "onlineId";
        public static string CustomerId = "cusId";
        public static string StartDate = "sd";
        public static string EndDate = "ed";
        public static string StoreId = "sId";
        public static string SizeId = "sizeId";
        public static string NoteId = "ntId";
        public static string ExpensId = "expensId";

        public static string DiscountId = "discountId";
        public static string ImportId = "importId";
        public static string ImportsDetailId = "importsDetailId";

        public static string DeliveryId = "deliveryId";
        public static string DeliveriesDetailId = "deliveriesDetailId";

        public static string ImportDeliveryTypeId = "idTypeId";

        public static string ReturnUrl = "rUrl";

        public static string Id         = "Id";
        public static string FullName   = "fn";
        public static string Email      = "email";
        public static string Content    = "content";

        public static string Keyword = "k";

        public static string PageIndex = "pi";
        public static string PageSize = "ps";

        public static string MinPrice = "minP";
        public static string MaxPrice = "maxP";        

        public static string SplitFormatTimeVn(string dt)
        {
            if (!string.IsNullOrEmpty(dt))
            {
                string[] splitDateTime = dt.Split('-');
                if (splitDateTime.Length > 0)
                    return string.Format("{0}-{1}-{2}", splitDateTime[2], splitDateTime[1], splitDateTime[0]);    
            }
            
            return string.Empty;
        }

        /// <summary>
        /// Ngăn cách hàng nghìn, Math Round 0, 0 trả về "-"            
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static string FormatPrice(decimal number)
        {
            string format;

            if (number == 0)
            {
                format = "-";
            }
            else
            {
                if (number >= 1000 || number <= -1000)
                {
                    format = String.Format("{0:0,0}", number);
                }
                else
                {
                    format = String.Format("{0:0}", number);
                }
            }

            try
            {
                format = format.Replace(",", ".");
            }
            catch { }

            return format;
        }

        public static int GetIdFromRequestString(string requestString, int defaultId)
        {
            requestString = HttpContext.Current.Request.QueryString[requestString];
            return requestString != null ? int.Parse(requestString) : defaultId;
        }

        public static string GetIdFromRequestString(string requestString, string defaultId)
        {
            requestString = HttpContext.Current.Request.QueryString[requestString];
            return requestString ?? defaultId;
        }

        public static DateTime GetIdFromRequestString(string requestString, DateTime defaultId)
        {
            requestString = HttpContext.Current.Request.QueryString[requestString];
            return requestString != null ? DateTime.Parse(requestString) : defaultId;
        }

        public static int GetStt(int dataItemIndex, int pageIndex, int pageSize)
        {
            return (pageSize * (pageIndex - 1) + dataItemIndex + 1);
        }

        public static string IsActive(bool isActive)
        {
            return isActive ? "<a class=\"btn14 topDir\" title=\"Đang hoạt động\"><img src='/images/icons/dark/check.png' title=\"Đang hoạt động\" alt=\"Đang hoạt động\"/></a>"
                            : "<a class=\"btn14 topDir\" title=\"Ngừng hoạt động\"><img src='/images/icons/dark/block.png' title=\"Ngừng hoạt động\"  alt=\"Ngừng hoạt động\"/></a>";
        }
        
        public static string IsStatus(int status)
        {
            switch (status)
            {
                case 0:
                    return "<img src='/images/icons/not-active.png'/>";
                case 1:
                    return "<img src='/images/icons/active.png'/>";
            }
            return string.Empty;
        }
    }
}
